/**
 * Created by MyPC on 2017/6/18.
 */
let express = require('express');
let router = express.Router();
let resultJson = require('../../common/resultJson');
let themesDao = require('../dao/themes');
// 获取数据
router.get('/', function (req, res, next) {
  let listByAll = themesDao.listByAll();
  listByAll.then(function (results, fields) {
    res.json(resultJson.create(1, 'success', results));
  }).catch(function (err) {
    console.log(err);
    res.json(resultJson.create('系统繁忙,请稍后再试'));
  });
})
router.get('/getAllByArticle', function (req, res, next) {
  let listByAll = themesDao.listByAll();
  listByAll.then(function (results, fields) {
    let map = {
      '-1':{
        id: -1,
        names: '根目录',
        hasChildren: 1,
        isPublic: 1,
        children: []
      }
    };
    for (let i=0;i<results.length;i++){
      map[results[i].id] = results[i];
    }
    for (let i=0;i<results.length;i++){
      if(results[i].parentId === -1){
        map['-1'].children.push(results[i]);
      } else {
        var obj = map[results[i].parentId];
        if (!Array.isArray(obj.children)) {
          obj.children = [];
        }
        obj.children.push(results[i]);
      }
    }
    // 判断,若文件夹下没有笔记本,该文件夹处于不能选择状态
    for (let i=0;i<results.length;i++){
      if(results[i].hasChildren && (!results[i].children || results[i].children.length==0 )){
        results[i].names = results[i].names+'(文件夹)';
        results[i].disabled = true;
      }
    }
    res.json(resultJson.create(1, 'success', [map['-1']]));
  }).catch(function (err) {
    console.log(err);
    res.json(resultJson.create('系统繁忙,请稍后再试'));
  });
})
// 新增
router.post('/', function (req, res, next) {
  let parentId = req.body.parentId;
  if (typeof parentId !== 'string' && parentId.length <= 0) {
    res.json(resultJson.create('请选择文件夹'));
    return;
  }
  let names = req.body.names;
  if (typeof names !== 'string' && names.length <= 0) {
    res.json(resultJson.create('请输入名称'));
    return;
  }
  let hasChildren = Number(req.body.hasChildren);
  if (hasChildren !== 1 && hasChildren !== 0) {
    res.json(resultJson.create('请选择类型'));
    return;
  }
  let isPublic = Number(req.body.isPublic);
  if (isPublic !== 1 && isPublic !== 0) {
    res.json(resultJson.create('请选择是否公开'));
    return;
  }
  let params = {
    parentId,
    names,
    hasChildren,
    isPublic,
    'remarks': req.body.remarks,
    'operatorId': 1
  }
  let save = themesDao.save(params);
  save.then(function (results, fields) {
    res.json(resultJson.create(1, 'success', results.insertId));
  }).catch(function (err) {
    console.log(err);
    res.json(resultJson.create('系统繁忙,请稍后再试'));
  });
})
// 修改
router.put('/', function (req, res, next) {
  let parentId = req.body.parentId;
  if (typeof parentId !== 'string' && parentId.length <= 0) {
    res.json(resultJson.create('请选择文件夹'));
    return;
  }
  let names = req.body.names;
  if (typeof names !== 'string' && names.length <= 0) {
    res.json(resultJson.create('请输入名称'));
    return;
  }
  let hasChildren = Number(req.body.hasChildren);
  if (hasChildren !== 1 && hasChildren !== 0) {
    res.json(resultJson.create('请选择类型'));
    return;
  }
  let isPublic = Number(req.body.isPublic);
  if (isPublic !== 1 && isPublic !== 0) {
    res.json(resultJson.create('请选择是否公开'));
    return;
  }
  let id = Number(req.body.id);
  if (id<1) {
    res.json(resultJson.create('请选择正确的数据'));
    return;
  }
  let params = {
    parentId,
    names,
    hasChildren,
    isPublic,
    'remarks': req.body.remarks,
    'operatorId': 1,
    id
  }
  let updateById = themesDao.updateById(params);
  updateById.then(function (results, fields) {
    res.json(resultJson.create(1, 'success', results.changedRows));
  }).catch(function (err) {
    console.log(err);
    res.json(resultJson.create('系统繁忙,请稍后再试'));
  });
})
module.exports = router;